package com.chenshu.shop.controller.index;

import com.chenshu.shop.common.PageUtil;
import com.chenshu.shop.common.SessionUtil;
import com.chenshu.shop.model.Cart;
import com.chenshu.shop.model.Good;
import com.chenshu.shop.model.Type;
import com.chenshu.shop.model.User;
import com.chenshu.shop.service.GoodService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-06-07
 * Time：08:46
 */
@Controller
@RequestMapping("/index")
public class GoodController_index {
    @Autowired
    GoodService goodService;
    @RequestMapping("/detail")
    public String goodsDetail(HttpServletRequest request, @RequestParam("id") Integer goodsId) {
        Good goods = goodService.getGoodById(goodsId);
        request.setAttribute("good", goods);
        return "forward:/index/detail.jsp";
    }

    @RequestMapping("/type")
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.SERIALIZABLE)
    public String getGoodsByType(HttpServletRequest request,
                                 @RequestParam(value = "id") Integer typeId,
                                 @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageIndex,
                                 @RequestParam(value = "size", defaultValue = "5", required = false) Integer pageSize) {
        //获取商品列表
        List<Good> goodList = goodService.getPageGoodsbyType(typeId, pageIndex, pageSize);
        //如果商品列表不为空，就给请求域存入一个type
        if (!goodList.isEmpty()) {
            Good good = goodList.get(0);
            Type type = new Type();
            type.setId(typeId);
            type.setName(good.getTypeName());
            request.setAttribute("type", type);
        }
        request.setAttribute("goodList", goodList);
        //获取商品个数
        Integer count = goodService.getGoodsCountByType(typeId);
        //生成分页条
        String pageTool = PageUtil.getPageHtml(request, count, pageIndex, pageSize);
        request.setAttribute("pageHtml", pageTool);
        return "forward:/index/goods.jsp";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        List<Good> todayList = goodService.getTop6Goods();
        request.setAttribute("todayList", todayList);
        List<Good> hotList = goodService.getHot8Goods();
        request.setAttribute("hotList", hotList);
        return "forward:/index/index.jsp";
    }

    @RequestMapping("/today")
    @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
    public String getMoreTop(HttpServletRequest request,
                        @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageIndex,
                        @RequestParam(value = "size", defaultValue = "5", required = false) Integer pageSize) {
        List<Good> goodList = goodService.getMoreTop(pageIndex, pageSize);
        request.setAttribute("goodList", goodList);

        Integer count = goodService.getTopCount();
        String pageTool = PageUtil.getPageHtml(request, count, pageIndex, pageSize);
        request.setAttribute("pageHtml", pageTool);
        return "forward:/index/goods.jsp";
    }

    @RequestMapping("/hot")
    public String getMoreHot(HttpServletRequest request,
                             @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageIndex,
                             @RequestParam(value = "size", defaultValue = "5", required = false) Integer pageSize) {
        List<Good> goodList = goodService.getMoreHot(pageIndex, pageSize);
        request.setAttribute("goodList", goodList);

        Integer count = goodService.getGoodsCount();
        String pageTool = PageUtil.getPageHtml(request, count, pageIndex, pageSize);
        request.setAttribute("pageHtml", pageTool);
        return "forward:/index/goods.jsp";
    }

    @RequestMapping("/search")
    public String getSearchGoods(HttpServletRequest request,
                                 String name,
                                 @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageIndex,
                                 @RequestParam(value = "size", defaultValue = "5", required = false) Integer pageSize) {
        List<Good> goodList = goodService.getSearchGoods(name, pageIndex, pageSize);
        request.setAttribute("goodList", goodList);

        Integer count = goodService.getSearchCount(name);
        String pageTool = PageUtil.getPageHtml(request, count, pageIndex, pageSize);
        request.setAttribute("pageHtml", pageTool);
        request.setAttribute("search", name);
        return "forward:/index/goods.jsp";
    }

    @RequestMapping("/new")
    public String toNew(HttpServletRequest request){
        List<Good> goodList = goodService.select10New();
        request.setAttribute("goodList",goodList);
        return "forward:/index/goods.jsp";
    }
}
